package com.danger.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.danger.Activity.R;
import com.danger.Model.FileInfo;
import com.danger.Service.DownloadService;
import com.danger.Widget.HorizontalProgressBarWithProgress;

import java.util.List;

/**
 * 文件列表适配器
 * Created by 俊 on 2016/6/9.
 */
public class FileListAdapter extends BaseAdapter {

    private Context context;
    private List<FileInfo> fileInfos;
    private LayoutInflater inflater;

    public FileListAdapter(Context context, List<FileInfo> fileInfos) {
        this.context = context;
        this.fileInfos = fileInfos;
        inflater = LayoutInflater.from(context);
    }

    @Override

    public int getCount() {
        return fileInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return fileInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //设置视图中的控件
        final FileInfo fileInfo = fileInfos.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            //加载视图
            convertView = inflater.inflate(R.layout.listview_item, null);
            holder = new ViewHolder(convertView);
            holder.name_textView.setText(fileInfo.getFileName());
            holder.horizontal_progressbar.setMax(100);
            holder.download_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, DownloadService.class);
                    intent.setAction(DownloadService.ACTION_START);
                    intent.putExtra("fileInfo", fileInfo);
                    context.startService(intent);
                }
            });
            holder.stop_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, DownloadService.class);
                    intent.setAction(DownloadService.ACTION_STOP);
                    intent.putExtra("fileInfo", fileInfo);
                    context.startService(intent);
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.horizontal_progressbar.setProgress(fileInfo.getFinished());

        return convertView;
    }

    static class ViewHolder {

        public TextView name_textView;
        public HorizontalProgressBarWithProgress horizontal_progressbar;
        public Button download_button;
        public Button stop_button;

        public ViewHolder(View view) {
            name_textView = (TextView) view.findViewById(R.id.name_textView);
            horizontal_progressbar = (HorizontalProgressBarWithProgress) view.findViewById(R.id.horizontal_progressbar);
            download_button = (Button) view.findViewById(R.id.download_button);
            stop_button = (Button) view.findViewById(R.id.stop_button);
        }
    }

    /**
     * 更新列表项中的进度条
     */
    public void updateProgress(int id, int progress) {
        FileInfo fileInfo = fileInfos.get(id);
        fileInfo.setFinished(progress);
        notifyDataSetChanged();
    }
}
